<template>
  <div class="list-table">
    <div class="list-table mb20" style="height:400px;width:100%;">
      <lineChart v-if="chartData.datas.length > 0" height="100%" :chart-data="chartData" />
      <div v-else class="noneData">
        <div>
          <svg-icon icon-class="none" class-name="none-icon" />
          <p>{{ $t('暂无数据') }}</p>
        </div>
      </div>
    </div>
    <div class="area-header">
      <span class="area-header-title">{{ $t('项目估值') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('是否上市')" prop="company">
        <el-select v-model="queryParams.company" :placeholder="$t('是否上市')" clearable size="small">
          <el-option
            v-for="dict in companyOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('估值时间')" prop="valuationTime">
        <DatePicker v-model="valuationTimes" type="daterange" />
      </el-form-item>
      <el-form-item :label="$t('币种')" prop="currency">
        <el-select v-model="queryParams.currency" :placeholder="$t('币种')" clearable size="small">
          <el-option
            v-for="dict in currencyOptions"
            :key="dict.dictValue"
            :label="$t(dict.dictLabel)"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t("搜索") }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t("重置") }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:companyvaluation:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t("新增") }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['deal:companyvaluation:export']"
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="exportHandle"
        >{{ $t("导出") }}</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" tooltip-effect="darkTable" :data="dealCompanyValuationList" border>
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('是否上市')" align="center" prop="company" :formatter="companyFormat" show-overflow-tooltip width="90" />
      <el-table-column :label="$t('减值预警')" align="center" show-overflow-tooltip width="90">
        <template slot-scope="scope">
          <div v-if="scope.row.earlyWarning" class="light-wrapper">
            <div class="light">
              <div class="shade" />
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('估值时间')" align="center" prop="valuationTime" show-overflow-tooltip width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.sourceChildId" class="link-type" @click="viewHandle(scope.row)">{{ parseTime(scope.row.valuationTime) }}</span>
          <span v-else class="link-type" @click="updateHandle(scope.row)">{{ parseTime(scope.row.valuationTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('公司整体估值') + $t('金额单位')" align="center" show-overflow-tooltip width="130" prop="companyValuation">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.companyValuation, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('持股比例(%)')" align="center" show-overflow-tooltip width="120" prop="shareholdingRatio" />
      <el-table-column :label="$t('项目估值') + $t('金额单位')" align="center" show-overflow-tooltip width="120" prop="projectValuation">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.projectValuation, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('币种')" align="center" show-overflow-tooltip width="90" prop="currency" :formatter="currencyFormat" />
      <el-table-column :label="$t('总投资成本') + $t('金额单位')" align="center" show-overflow-tooltip width="120" prop="totalInvestmentCost">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.totalInvestmentCost, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('估值背景')" align="center" prop="valuationBackground" width="90" show-overflow-tooltip />
      <el-table-column :label="$t('应对方案')" align="center" prop="responsePlan" width="90" show-overflow-tooltip />
      <el-table-column :label="$t('估值方法')" align="center" prop="valuationMethod" :formatter="valuationMethodFormat" width="90" />
      <el-table-column :label="$t('可比公司/交易法')" align="center" prop="comparableCompanyLaw" :formatter="comparableCompanyLawFormat" width="130" />
      <el-table-column :label="$t('倍数')" align="center" show-overflow-tooltip width="90" prop="multiple" />
      <el-table-column :label="$t('净利润/营业收入') + $t('金额单位')" align="center" width="150" prop="netProfit">
        <template slot-scope="scope">
          <span class="money-text">{{ moneyFormat(scope.row.netProfit, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" :label="$t('操作')" align="center" class-name="small-padding fixed-width" width="200">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['deal:equityChange:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="copyHandle(scope.row)"
          >{{ $t('复制') }}</el-button>
          <el-button
            v-if="scope.row.sourceChildId"
            v-hasPermi="['deal:equityChange:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="editHandle(scope.row)"
          >{{ $t('编辑') }}</el-button>
          <el-button
            v-if="scope.row.sourceChildId"
            v-hasPermi="['deal:companyvaluation:edit']"
            size="small"
            type="text"
            @click="viewHandle(scope.row)"
          >
            <el-tooltip :content="$t('来自capTable同步的数据')">
              <i class="el-icon-question" />
            </el-tooltip>
            {{ $t('查看') }}
          </el-button>
          <el-button
            v-if="!scope.row.sourceChildId"
            v-hasPermi="['deal:companyvaluation:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-if="!scope.row.sourceChildId"
            v-hasPermi="['deal:companyvaluation:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公司估值对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :destroy-on-close="true" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealCompanyValuationForm ref="dealCompanyValuationForm" />
    </el-dialog>

    <!-- 快速打开 capTable -->
    <el-dialog :title="'CapTable'" :visible.sync="capTableOpen" :destroy-on-close="true" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealEquityChangeForm ref="dealEquityChangeForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealCompanyValuation, delDealCompanyValuation } from '@/api/deal/companyvaluation/dealCompanyValuation'
import dealCompanyValuationForm from './dealCompanyValuationForm.vue'
import dealEquityChangeForm from '@/views/deal/equitychange/dealEquityChangeForm'
import lineChart from '@/views/chart/lineChart.vue'
import { companyValuationChart } from '@/api/deal/companyvaluation/dealCompanyValuation'
export default {
    components: {
      dealCompanyValuationForm,
      dealEquityChangeForm,
      lineChart
    },
  data() {
      return {
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 公司估值表格数据
          dealCompanyValuationList: [],
          // 弹出层标题
          title: '项目估值',
          // 是否显示弹出层
          formOpen: false,
        capTableOpen: false,
        // 估值时间 参数
        valuationTimes: [],
        // 是否上市 字典sys_yes_no字典
        companyOptions: [],
        // 币种 字典currency字典
        currencyOptions: [],
        // 估值方法字典
        valuationMethodOptions: [],
        // 可比公司法字典
        comparableCompanyLawOptions: [],
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              company: undefined,
              valuationTime: undefined,
              currency: undefined,
              startDate: undefined,
              endDate: undefined,
              quarterOrMonth: false,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
          chartData: {
            datas: []
          }
      }
  },
  created() {
      this.getList()
      this.companyValuationChart()
      this.getDicts('sys_yes_no').then(response => {
          this.companyOptions = response.data
      })
      this.getDicts('currency').then(response => {
          this.currencyOptions = response.data
      })
      this.getDicts('valuation_method').then(response => {
          this.valuationMethodOptions = response.data
      })
      this.getDicts('comparable_company').then(response => {
          this.comparableCompanyLawOptions = response.data
      })
  },
  methods: {
    /** 查询公司估值列表 */
    getList() {
        this.loading = true
        if (this.valuationTimes != null && this.valuationTimes.length > 1) {
          this.queryParams.startDate = this.valuationTimes[0]
          this.queryParams.endDate = this.valuationTimes[1]
        } else {
          this.queryParams.startDate = undefined
          this.queryParams.endDate = undefined
        }
        listDealCompanyValuation(this.queryParams).then(response => {
            this.dealCompanyValuationList = response.rows
            this.total = response.total
            this.loading = false
        })
    },
    // 折线图数据
    companyValuationChart() {
      companyValuationChart(this.queryParams).then(response => {
        this.chartData = {
          legend: true,
          datas: [],
          valueFormatter: true,
          legendLeft: 'center',
          xLabel: []
        }
        if (response.data != null) {
          this.chartData.xLabel = response.data.dateList
          this.chartData.datas = [
            {
              name: this.$t('公司整体估值'),
              type: 'line',
              stack: '1',
              data: response.data.companyValuationList
            },
            {
              name: this.$t('项目估值'),
              type: 'line',
              stack: '1',
              data: response.data.projectValuationList
            }
          ]
        }
      })
      this.$nextTick(() => {
        this.$refs.valuationChart.chart.resize()
      })
    },
    // 是否上市 字典sys_yes_no字典翻译
    companyFormat(row) {
      return this.selectDictLabel(this.companyOptions, row.company)
    },
    // 币种 字典currency字典翻译
    currencyFormat(row) {
      return this.selectDictLabel(this.currencyOptions, row.currency)
    },
    // 估值方法字典翻译
    valuationMethodFormat(row) {
      return this.selectDictLabel(this.valuationMethodOptions, row.valuationMethod)
    },
    // 可比公司法字典翻译
    comparableCompanyLawFormat(row) {
      if (row.valuationMethod === 'JY') {
        return row.comparableCompanyLaw
      }
      return this.selectDictLabel(this.comparableCompanyLawOptions, row.comparableCompanyLaw)
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            company: undefined,
            valuationTime: undefined,
            numberSharesHeld: undefined,
            dailySharePrice: undefined,
            companyValuation: undefined,
            shareholdingRatio: undefined,
            projectValuation: undefined,
            currency: undefined,
            totalInvestmentCost: undefined,
            valuationBackground: undefined,
            responsePlan: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined,
            delFlag: undefined,
            valuationMethod: undefined,
            comparableCompanyLaw: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
        this.resetForm('queryForm')
        this.valuationTimes = []
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.title = '新增项目估值'
        this.$nextTick(() => {
            this.$refs.dealCompanyValuationForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.title = '修改项目估值'
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealCompanyValuationForm.init(row)
            this.$refs.dealCompanyValuationForm.$refs.form.clearValidate()
        })
    },
    viewHandle(row) {
      this.formOpen = true
      this.$nextTick(() => {
        // 调用form页面的 init 方法初始化数据
        this.$refs.dealCompanyValuationForm.init(row)
        this.$refs.dealCompanyValuationForm.$refs.form.clearValidate()
        this.$refs.dealCompanyValuationForm.btnShow = false
      })
    },
    // 跳转capTable 界面
    editHandle(row) {
      this.capTableOpen = true
      this.$nextTick(() => {
        this.$refs.dealEquityChangeForm.init({ id: row.sourceChildId })
      })
    },
    // 复制
    copyHandle(row) {
      this.formOpen = true
      this.title = '复制项目估值'
      this.$nextTick(() => {
        this.$refs.dealCompanyValuationForm.init(row, 'copy')
      })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(function() {
            return delDealCompanyValuation(row.id)
        }).then(() => {
            this.getList()
            this.companyValuationChart()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(function() {
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        this.downLoadExcel('/companyvaluation/dealCompanyValuation/export', this.queryParams)
    }
  }
}
</script>
<style lang="scss" scoped>
  .circle{
    width: 100px;
    height: 100px;
    border-radius: 50%;
    background-color: red;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .light-wrapper { display: flex; justify-content: center; align-items: center; height: 100%; }
  .light { width: 20px; height: 20px; border-radius: 50%; background-color: red; }
  .shade {
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background-color: gray;
    position: absolute;
    top: -10px;
    left: -10px;
    z-index: -1;
  }
</style>
